﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Net;

namespace DIMP_Libraries
{
    public class ClientServeur
    {
        private TcpClient _tc;
        private NetworkStream _ns;
        private BinaryFormatter _bf;
        private Thread _readMessage;
        public Boolean stopThread;
        public event EventHandler<MessageEventArg> onMessageReceived;

        private String _pseudo;

        public String Pseudo
        {
            get { return _pseudo; }
            set { _pseudo = value; }
        }
        

        /*
         * Constructeur de la partie serveur
         */
        public ClientServeur(TcpClient client)
        {
            this._tc = client;

            _ns = _tc.GetStream();
            _bf = new BinaryFormatter();

            _readMessage = new Thread(checkMessage);
            _readMessage.Start();

            stopThread = false;
        }

        /*
         * Constructeur de la partie client
         */
        public ClientServeur(String ip , Int32 port)
        {
            TcpClient client = new TcpClient();
            try
            {
                client.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
                Logger.LogSuccess("Connexion : OK");
            }
            catch (Exception)
            {
                Logger.LogError("Connexion : FAIL");
                return;
            }

            this._tc = client;

            _ns = _tc.GetStream();
            _bf = new BinaryFormatter();

            _readMessage = new Thread(checkMessage);
            _readMessage.Start();

            stopThread = false;
        }

        /*
         * Retourne le client
         */
        public TcpClient getTcpClient()
        {
            return this._tc;
        }

        /*
         * Envoie un message générique
         */
        public virtual void SendMessage(object msg)
        {
            //try
            //{
                _ns = _tc.GetStream();
                _bf.Serialize(_ns, msg);
                _ns.Flush();
            //}
            //catch (Exception)
            //{
            //    Logger.LogError("Erreur dans sendmessage(), noraj");
            //}
        }

        /*
         * Thread de traitement des messages reçu
         */
        private void checkMessage()
        {
            while (true && !stopThread )
            {
                //try
                //{
                    //_ns = _tc.GetStream();
                    if (_ns.DataAvailable)
                    {
                        object msg = (object)_bf.Deserialize(_ns);

                        MessageEventArg mea = new MessageEventArg();
                        mea.Message = msg;
                        mea.Client = this;
                        if (onMessageReceived != null)
                            onMessageReceived(this, mea);
                    }

                    Thread.Sleep(20);
                //}
                //catch (Exception)
                //{ }
            }
        }
    }
}
